//
//  NurserySchoolCellTableViewCell.m
//  TestDIYCell
//
//  Created by 刘泽琛 on 14-5-15.
//  Copyright (c) 2014年 刘 泽琛. All rights reserved.
/*
 // 图片模式
 imgView.clipsToBounds = YES;
 imgView.contentMode = UIViewContentModeScaleAspectFill;
 */


#define KINDER_SECTION [[NSUserDefaults standardUserDefaults] integerForKey:USERDEFAULTS_KINDERSECTION]
#define MIN_X KINDER_SECTION == INTRODUCTION ? 15 :10
#define CONTENT_WIDTH KINDER_SECTION == INTRODUCTION ? 290 :300
#define TITLE_FONT KINDER_SECTION == INTRODUCTION ? 22 :18
#define CONTENT_FONT KINDER_SECTION == INTRODUCTION ? 17 :15

#import "NurserySchoolCellTableViewCell.h"

@implementation NurserySchoolCellTableViewCell {
    // 当前是幼儿园哪个版块
    NSInteger kinderSchoolSection;
    BN_KinderGartenHelper *helper_;
    MJPhoto *photo_;
    MJPhotoBrowser *browser_;
}

#pragma mark
#pragma mark 初始化
- (void)awakeFromNib
{
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    kinderSchoolSection = [[NSUserDefaults standardUserDefaults] integerForKey:USERDEFAULTS_KINDERSECTION];
    switch (kinderSchoolSection) {
        case INTRODUCTION:
            self.deleBtn.hidden = YES;
            self.browseFullTextBtn.hidden = YES;
            self.lineLabel.hidden = YES;
            break;
        case ACTIVITY:
            // 有最新图标
            break;
        case FOOD:
            // 没有浏览全文且无标题
            self.browseFullTextBtn.hidden = YES;
            break;
        case SUGGESTIONBOX:
            // 没有内容图片
            break;
        default:
            break;
    }
    
    // 删除按钮
    [self.deleBtn addTarget:self action:@selector(kinderSchoolCellDeteAction) forControlEvents:UIControlEventTouchUpInside];
    // 浏览全文按钮
    [self.browseFullTextBtn addTarget:self action:@selector(browseFullTextAction) forControlEvents:UIControlEventTouchUpInside];

    // helper
    if (!helper_) {
        helper_ = [[BN_KinderGartenHelper alloc]init];
    }
    
    // 图片模式
    self.contImageView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    self.contImageView.clipsToBounds = YES;
    self.contImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.contImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *browseBigImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(browseBigImageAction)];
    [self.contImageView addGestureRecognizer:browseBigImageTap];
}

// 删除
- (void)kinderSchoolCellDeteAction {
    switch (kinderSchoolSection) {
        case INTRODUCTION:
            break;
        case ACTIVITY:
        {
            UIAlertView *tishiView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否要删除此条活动？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            tishiView.tag = 401;
            [tishiView show];
            break;
        }
            break;
        case FOOD:
        {
            UIAlertView *tishiView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否要删除此条美食？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            tishiView.tag = 402;
            [tishiView show];
            break;
        }
        case SUGGESTIONBOX:
        {
            UIAlertView *tishiView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否要删除此条意见？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            tishiView.tag = 403;
            [tishiView show];
            break;
        }
            break;
        default:
            break;
    }
}

// 华丽的顶部分割线
- (void)initDividerView {
    self.dividerView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    UILabel *titleLabel = (UILabel *)[self.dividerView viewWithTag:402];
    titleLabel.backgroundColor = APP_KINDER_TIME_LABEL_BGCOLOR;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.layer.masksToBounds = YES;
    [titleLabel.layer setCornerRadius:9];
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleLabel sizeToFit];
    titleLabel.frame = CGRectMake((SCREEN_WIDTH-CGRectGetWidth(titleLabel.frame))/2-10, (35-CGRectGetHeight(titleLabel.frame))/2-1.5, CGRectGetWidth(titleLabel.frame)+20, CGRectGetHeight(titleLabel.frame)+3);
}

- (void)toGroup {
    // title
    self.titleLabel.numberOfLines = 0;
    UIFont * tfont = [UIFont fontWithName:@"TrebuchetMS-Bold" size:TITLE_FONT];
    self.titleLabel.font = tfont;
    self.titleLabel.lineBreakMode = NSLineBreakByCharWrapping ;
    CGSize size = CGSizeMake(CONTENT_WIDTH,10000);
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize  actualsize = [self getTextSize:size disFont:tfont contentStr:self.titleLabel.text contextDic:tdic];
    
    NSInteger object_space = 0;
    switch (kinderSchoolSection) {
        case INTRODUCTION:
            object_space = 18;
            break;
        default:
            object_space = 10;
            break;
    }
    self.titleLabel.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.dividerView.frame)+object_space, CONTENT_WIDTH, actualsize.height);
    [self.titleLabel sizeToFit];
    
    // 内容图片
    if (kinderSchoolSection == SUGGESTIONBOX) {
        self.contImageView.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.titleLabel.frame)+10, CONTENT_WIDTH, 0) ;

    }else {
        switch (kinderSchoolSection) {
            case INTRODUCTION:
                self.contImageView.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.titleLabel.frame)+15, CONTENT_WIDTH, 150);
                break;
            case ACTIVITY:
                self.contImageView.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.titleLabel.frame)+10, CONTENT_WIDTH, 150);
                break;
            case FOOD:
                self.contImageView.frame = CGRectMake(MIN_X, CGRectGetMaxY(self.dividerView.frame)+10, CONTENT_WIDTH, 150);
                break;
            default:
                break;
        }
    }

    // 内容文字
    self.contLabel.numberOfLines = 0;
    UIFont *contLabelfont = [UIFont systemFontOfSize:CONTENT_FONT];
    self.contLabel.font = contLabelfont;
    self.contLabel.lineBreakMode = NSLineBreakByCharWrapping ;
    CGSize contLabelsize = CGSizeMake(CONTENT_WIDTH,10000);
    NSDictionary *contLabeldic = [NSDictionary dictionaryWithObjectsAndKeys:contLabelfont,NSFontAttributeName,nil];
    CGSize contLabelActualsize = [self getTextSize:contLabelsize disFont:contLabelfont contentStr:self.contLabel.text contextDic:contLabeldic];
    
    // 内容Label
    if (kinderSchoolSection == SUGGESTIONBOX) {
        self.contLabel.frame = CGRectMake( MIN_X, CGRectGetMaxY(self.contImageView.frame), CONTENT_WIDTH, contLabelActualsize.height);
    }else {
        self.contLabel.frame = CGRectMake( MIN_X, CGRectGetMaxY(self.contImageView.frame)+10, CONTENT_WIDTH, contLabelActualsize.height);
    }
    [self.contLabel sizeToFit];
    [self dealLineDeleBrow:kinderSchoolSection];
}

- (void)toMine_Parent_Group {
    // 顶部分割线
    self.dividerView.hidden = YES;
    self.contImageView.backgroundColor = [UIColor clearColor];

    // 内容文字
    self.contLabel.numberOfLines = 0;
    UIFont * contLabelfont = [UIFont systemFontOfSize:15];
    self.contLabel.font = contLabelfont;
    self.contLabel.lineBreakMode = NSLineBreakByCharWrapping ;
    CGSize contLabelsize = CGSizeMake(CONTENT_WIDTH,10000);
    NSDictionary * contLabeldic = [NSDictionary dictionaryWithObjectsAndKeys:contLabelfont,NSFontAttributeName,nil];
    CGSize contLabelActualsize = [self getTextSize:contLabelsize disFont:contLabelfont contentStr:self.contLabel.text contextDic:contLabeldic];
    // 内容Label
    self.contLabel.frame = CGRectMake( MIN_X, 10, CONTENT_WIDTH, contLabelActualsize.height);
    
    // 分割线
    self.lineLabel.frame = CGRectMake(0, CGRectGetMaxY(self.contLabel.frame)+10, SCREEN_WIDTH, 1);
    self.lineLabel.alpha = 0.3;
    
    // 浏览全文
    self.browseFullTextBtn.frame = CGRectMake(-28, CGRectGetMaxY(self.contLabel.frame)+20, 100, 24);
    self.browseFullTextBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.browseFullTextBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    self.cellHeight = CGRectGetMaxY(self.browseFullTextBtn.frame)+10;
    
    // 隐藏删除按钮
    self.deleBtn.hidden = YES;
}

#pragma mark
#pragma mark Action
// 全文
- (void)browseFullTextAction {
    
    // kinderSchoolSection == 10 为当前显示界面不是在幼儿园
    if (kinderSchoolSection == 10) {
        // 记录是老师状态
        NSInteger mineSection = [[NSUserDefaults standardUserDefaults]integerForKey:USERDEFAULTS_MINE_PARENT_TEACHER];
        switch (mineSection) {
            case MINE_TEACHER_LOOK_COMMENT:
                [[NSNotificationCenter defaultCenter]postNotificationName:KMINE_TEACHER_LOOK_FULLTEXT object:nil];
                break;
            case MINE_PARENT_LOOK_COMMENT:
                [[NSNotificationCenter defaultCenter]postNotificationName:KMINE_PARENT_LOOK_FULLTEXT object:nil];
                break;
            default:
                break;
        }
    }else {
        switch (kinderSchoolSection) {
            case INTRODUCTION:
                break;
            case ACTIVITY:
                [[NSNotificationCenter defaultCenter]postNotificationName:KKINDER_OPEN_ACTIVITY_XQ object:self.activityId];
                break;
            case FOOD:
                [[NSNotificationCenter defaultCenter]postNotificationName:KKINDER_OPEN_FOOD_XQ object:self.foodId];
                break;
            case SUGGESTIONBOX:
                [[NSNotificationCenter defaultCenter]postNotificationName:KKINDER_OPEN_BOX_XQ object:self.boxId];
                break;
            default:
                break;
        }
    }
}

- (void)browseBigImageAction {
    
    if (kinderSchoolSection == FOOD) {
        NSMutableArray *photos = [NSMutableArray arrayWithCapacity:1];
        photo_ = [[MJPhoto alloc] init];
        photo_.image = self.contImageView.image;
        photo_.srcImageView = self.contImageView;
        [photos addObject:photo_];
        
        browser_ = [[MJPhotoBrowser alloc] init];
        browser_.currentPhotoIndex = 0;
        browser_.photos = photos;
        [browser_ show];
    }
}

#pragma mark
#pragma mark 分割线 删除 浏览全文按钮
- (void)dealLineDeleBrow:(NSInteger)section {
    BN_UserInfo *userInfo = [BN_UserHelper unArchiverAccountInfo];
    NSInteger userType = [userInfo.userType integerValue];
    switch (userType) {
        case ENUM_ROLE_LEADER:
        {
            // 分割线
            self.lineLabel.frame = CGRectMake(0, CGRectGetMaxY(self.contLabel.frame)+10, SCREEN_WIDTH, 1);
            self.lineLabel.alpha = 0.3;
            
            // 删除按钮
            self.deleBtn.frame = CGRectMake(5, CGRectGetMaxY(self.contLabel.frame)+15, 24, 24);
            if (section == SUGGESTIONBOX || section == INTRODUCTION) {
                self.deleBtn.hidden = YES;
            }else {
                self.deleBtn.hidden = NO;
            }
            
            // 浏览全文
            self.browseFullTextBtn.frame = CGRectMake(210, CGRectGetMaxY(self.contLabel.frame)+15, 100, 24);
            self.browseFullTextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
            self.cellHeight = CGRectGetMaxY(self.browseFullTextBtn.frame)+5;
        }
            break;
        case ENUM_ROLE_CLASS_TEACHER:
        {
            if (section == FOOD) {
                // 分割线
                self.lineLabel.frame = CGRectMake(0, CGRectGetMaxY(self.contLabel.frame)+10, SCREEN_WIDTH, 1);
                self.lineLabel.hidden = YES;
                
                self.deleBtn.frame = CGRectMake(5, CGRectGetMaxY(self.contLabel.frame)+15, 24, 0);
                self.deleBtn.hidden = YES;
                
                // 浏览全文
                self.browseFullTextBtn.frame = CGRectMake(210, CGRectGetMaxY(self.contLabel.frame)+15, 100, 0);
                self.browseFullTextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
                self.cellHeight = CGRectGetMaxY(self.browseFullTextBtn.frame)-5;
            }else {
                // 分割线
                self.lineLabel.frame = CGRectMake(0, CGRectGetMaxY(self.contLabel.frame)+10, SCREEN_WIDTH, 1);
                self.lineLabel.alpha = 0.3;
                
                // 删除按钮
                self.deleBtn.frame = CGRectMake(5, CGRectGetMaxY(self.contLabel.frame)+15, 24, 24);
                self.deleBtn.hidden = YES;
                
                // 浏览全文
                self.browseFullTextBtn.frame = CGRectMake(210, CGRectGetMaxY(self.contLabel.frame)+15, 100, 24);
                self.browseFullTextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
                self.cellHeight = CGRectGetMaxY(self.browseFullTextBtn.frame)+5;
            }
        }
            break;
        case ENUM_ROLE_PARENT:
        {
            if (section == FOOD) {
                // 分割线
                self.lineLabel.frame = CGRectMake(0, CGRectGetMaxY(self.contLabel.frame)+10, SCREEN_WIDTH, 1);
                self.lineLabel.hidden = YES;
                
                self.deleBtn.frame = CGRectMake(5, CGRectGetMaxY(self.contLabel.frame)+15, 24, 0);
                self.deleBtn.hidden = YES;
                
                // 浏览全文
                self.browseFullTextBtn.frame = CGRectMake(210, CGRectGetMaxY(self.contLabel.frame)+15, 100, 0);
                self.browseFullTextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
                self.cellHeight = CGRectGetMaxY(self.browseFullTextBtn.frame)-5;
            }else {
                // 分割线
                self.lineLabel.frame = CGRectMake(0, CGRectGetMaxY(self.contLabel.frame)+10, SCREEN_WIDTH, 1);
                self.lineLabel.alpha = 0.3;
                
                // 删除按钮
                self.deleBtn.frame = CGRectMake(5, CGRectGetMaxY(self.contLabel.frame)+15, 24, 24);
                if (section == SUGGESTIONBOX) {
                    self.deleBtn.hidden = NO;
                }else {
                    self.deleBtn.hidden = YES;
                }
                
                // 浏览全文
                self.browseFullTextBtn.frame = CGRectMake(210, CGRectGetMaxY(self.contLabel.frame)+15, 100, 24);
                self.browseFullTextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
                self.cellHeight = CGRectGetMaxY(self.browseFullTextBtn.frame)+5;
            }
        }
            break;
        default:
            break;
    }

}

// 封装自动撑开高度的方法
- (CGSize)getTextSize:(CGSize)provisionsSize disFont:(UIFont *)font contentStr:(NSString *)contentStr contextDic:(NSDictionary *)contextDic {
    
    CGSize size;
    switch (MAIN_VERSION) {
        case 6:
            size = [contentStr sizeWithFont:font constrainedToSize:provisionsSize lineBreakMode:NSLineBreakByWordWrapping];
            break;
        default:
            
            size =[contentStr boundingRectWithSize:provisionsSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:contextDic context:nil].size;
            break;
    }
    return size;
}

#pragma mark
#pragma mark UIAlertView

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSString *tagStr = [NSString stringWithFormat:@"%ld",(long)self.tag];
    
    if (alertView.tag == 401) {
        if (buttonIndex == 1) {
            NSString *actiId = self.activityId;
            [helper_ removeActionById:actiId completeHandler:^(id result, NSString *errMsg) {
                if (errMsg) {
                }else{
                    NSLog(@"删除活动：%@",result);
                    [[NSNotificationCenter defaultCenter]postNotificationName:K_SCHOOL_ACTIVITY_DELE object:tagStr];
                }
            }];
        }
    }
    
    if (alertView.tag == 402) {
        if (buttonIndex == 1) {
            [helper_ removeFoodById:self.foodId completeHandler:^(id result, NSString *errMsg) {
                if (errMsg) {
                }else{
                    NSLog(@"删除美食：%@",result);
                    [[NSNotificationCenter defaultCenter]postNotificationName:K_SCHOOL_FOOD_DELE object:tagStr];
                }
            }];
        }
    }
    
    if (alertView.tag == 403) {
        if (buttonIndex == 1) {
            [helper_ removeSuggestById:self.boxId completeHandler:^(id result, NSString *errMsg) {
                if (errMsg) {
                }else{
                    NSLog(@"删除编号为%@的园长信箱：%@",self.boxId,result);
                    [[NSNotificationCenter defaultCenter]postNotificationName:K_SCHOOL_BOX_DELE object:tagStr];
                }
            }];
        }
    }
}

- (void)dealloc
{
    // 幼儿园
    [[NSNotificationCenter defaultCenter]removeObserver:self name:K_SCHOOL_ACTIVITY_DELE object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:K_SCHOOL_FOOD_DELE object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:K_SCHOOL_BOX_DELE object:nil];
    // 详情
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KKINDER_OPEN_ACTIVITY_XQ object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KKINDER_OPEN_FOOD_XQ object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KKINDER_OPEN_BOX_XQ object:nil];
    // 我的
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KMINE_PARENT_LOOK_FULLTEXT object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:KMINE_TEACHER_LOOK_FULLTEXT object:nil];
    
    photo_ = nil;
    browser_ = nil;
    self.foodId = nil;
    self.activityId = nil;
    self.boxId = nil;
    
    NSLog(@"幼儿园样式Cell 释放...");
}
@end
